home *** CD-ROM | disk | FTP | other *** search
- Path: news.PBI.net!usenet
- From: mich@pbinet.com
- Newsgroups: comp.lang.c
- Subject: Re: Q: How to assign structures?
- Date: 9 Mar 1996 17:09:35 GMT
- Organization: Pacific Bell Internet Services
- Message-ID: <4hse0f$efq@SNFC21_SRVR_WWW.PBI.net>
- References: <4gsalu$dll@ccshst05.cs.uoguelph.ca>
- Reply-To: mich@pbinet.com
- NNTP-Posting-Host: ppp-5-36.rdcy01.pbinet.com
- X-Newsreader: IBM NewsReader/2 v1.03
-
- In <4gsalu$dll@ccshst05.cs.uoguelph.ca>, thay@uoguelph.ca (Toby K Hay) writes:
- >I need to assign values to structures in one array from structures in
- >another array - in essence I'm copying the whole structure from one array
- >to the other. Can I use simple assignment like this:
-
- >struct StructName {
- > int IVal;
- > float FVal;}
-
- >struct StructName Array1[5], Array2[5];
- >.. . .
- >for (i=0;i<5;i++) Array1[i] = Array2[i];
- >.. . .
- >And if not that, can I use memcpy() and sizeof() to do the assignment:
-
- >for (i=0;i<5;i++) memcpy(&Array2[i],&Array1[i],sizeof(struct StructName));
- >Or do I have to assign each element of the structure?
-
- It would be a lot easier to create a pointer to the struct and then make a copy
- of the pointer;
-
- struct mystruct {..} ms, *ps;
-
- main()
- {
- void *ns;
-
- ps = &ms;
- ns = ps;
- }
-
- This is an overly simple example, you might want to malloc the new pointer or
- something, I dunno, but this a basic approach.
-
-